#include <cstdio>
#include <cstring>

int n, st1[ 6 ], st2[ 6 ];
bool ag[ 6 ][ 6 ], visit1[ 6 ], visit2[ 6 ], flag, ok;
char s[ 6 ][ 25 ];

void init( )
{
    int i, j, m;
    char tp[ 25 ];
    scanf("%d", &n);
    memset( ag, 0, sizeof( ag ) );
    for ( i = 0; i < n; i++ )
        scanf("%s", s[ i ]);
    for ( i = 0; i < n - 1; i++ )
        for ( j = i + 1; j < n; j++ )
            if ( strcmp( s[ j ], s[ i ] ) < 0 )
            {
                strcpy( tp, s[ j ] );
                strcpy( s[ j ], s[ i ] );
                strcpy( s[ j ], tp );
            }
    for ( i = 0; i < n; i++ )
    {
        scanf("%d", &m);
        while ( m-- )
        {
            scanf("%s", tp);
            for ( j = 0; j < n; j++ )
                if ( strcmp( tp, s[ j ] ) == 0 )
                {
                    ag[ j ][ i ] = 1;
                    break;
                }
        }
    }
    memset( visit1, 0, sizeof( visit1 ) );
    ok = false;
}

void dfs2( int step )
{
    int i, j;
    if ( step == n )
    {
        i = 0; j = 0;
        while ( i < n )
        {
            while ( j < n && !ag[ st1[ i ] ][ st2[ j ] ] ) j++;
            if ( j >= n ) break;
            i++;
        }
        if ( i >= n ) flag = false;
    }
    else
    {
        for ( i = 0; i < n; i++ )
            if ( !visit2[ i ] )
            {
                visit2[ i ] = 1;
                st2[ step ] = i;
                dfs2( step + 1 );
                visit2[ i ] = 0;
            }
    }
}

void dfs1( int step )
{
    int i;
    if ( ok ) return ;
    if ( step == n )
    {
        flag = true;
        memset( visit2, 0, sizeof( visit2 ) );
        dfs2( 0 );
        if ( flag )
        {
            ok = true;
            return ;
        }
    }
    else
    {
        for ( i = 0; i < n && !ok; i++ )
            if ( !visit1[ i ] )
            {
                visit1[ i ] = 1;
                st1[ step ] = i;
                dfs1( step + 1 );
                visit1[ i ] = 0;
            }
    }
}

void print( )
{
    int i;
    if ( !ok )
    {
        puts("No");
        return ;
    }
    puts("Yes");
    printf("%s", s[ st1[ 0 ] ]);
    for ( i = 1; i < n; i++ )
        printf(" %s", s[ st1[ i ] ]);
    printf("\n");
}

int main( )
{
    int t, cas =1;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        dfs1( 0 );
        printf("Case %d: ", cas++);
        print( );
    }
    return 0;
}
